#include "fenrecognizer.h"
#include <QString>
#include <QStringList>
#include <QChar>
#include <QDebug>
#include <board.h>
#include <piece.h>
#include <rook.h>
#include <bishop.h>
#include <knight.h>
#include <pawn.h>
#include <king.h>
#include <queen.h>

FENRecognizer::FENRecognizer(QObject *parent) : QObject(parent)
{

}

void FENRecognizer::recognize(QString fen, Board *board)
{
    QStringList splitted_fen = fen.split(" ");
    QStringList rows = splitted_fen[0].split("/");
    for (int i = 0; i < rows.size(); i++){
        int x = 1;
        int y = i+1;
        foreach (QChar c, rows.at(i)){
            if (x > 8)break;
            Piece *piece;
            switch (c.toLatin1()) {
            case 'r':
                piece = new Rook(board->getCell(x,y), "W");
                board->getLayout()->addWidget(piece);
                break;
            case 'b':
                piece = new Bishop(board->getCell(x,y), "W");
                board->getLayout()->addWidget(piece);
                break;
            case 'n':
                piece = new Knight(board->getCell(x,y), "W");
                board->getLayout()->addWidget(piece);
                break;
            case 'p':
                piece = new Pawn(board->getCell(x,y), "W");
                board->getLayout()->addWidget(piece);
                break;
            case 'k':
                piece = new King(board->getCell(x,y), "W");
                board->getLayout()->addWidget(piece);
                break;
            case 'q':
                piece = new Queen(board->getCell(x,y), "W");
                board->getLayout()->addWidget(piece);
                break;
            case 'R':
                piece = new Rook(board->getCell(x,y), "B");
                board->getLayout()->addWidget(piece);
                break;
            case 'B':
                piece = new Bishop(board->getCell(x,y), "B");
                board->getLayout()->addWidget(piece);
                break;
            case 'N':
                piece = new Knight(board->getCell(x,y), "B");
                board->getLayout()->addWidget(piece);
                break;
            case 'P':
                piece = new Pawn(board->getCell(x,y), "B");
                board->getLayout()->addWidget(piece);
                break;
            case 'K':
                piece = new King(board->getCell(x,y), "B");
                board->getLayout()->addWidget(piece);
                break;
            case 'Q':
                piece = new Queen(board->getCell(x,y), "B");
                board->getLayout()->addWidget(piece);
                break;
            }
            if (c.isDigit()){
                x += c.digitValue();
            }else{
                x += 1;
            }
        }
    }
}
